package com.dimples.security.service;

import com.dimples.common.constant.StrPool;
import com.dimples.common.entity.SecurityUser;
import com.dimples.common.service.DimplesJwtTokenServiceImpl;
import com.dimples.common.util.HttpContextUtil;

import org.springframework.stereotype.Service;

import java.util.Set;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

/**
 * 自定义权限注解
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/7/20
 */
@Service("auth")
@RequiredArgsConstructor
public class DimplesPermissionService {

    /**
     * 所有权限标识
     */
    private static final String ALL_PERMISSION = "*:*:*";

    /**
     * 管理员角色权限标识
     */
    private static final String SUPER_ADMIN = "admin";

    private final DimplesJwtTokenServiceImpl tokenService;

    /**
     * 验证用户是否具备某权限
     *
     * @param permission 权限字符串
     * @return 用户是否具备某权限
     */
    public boolean hasPerm(String permission) {
        if (StrUtil.isBlank(permission)) {
            return false;
        }
        SecurityUser securityUser = tokenService.getLoginUserFromToken(HttpContextUtil.getRequest());
        if (ObjectUtil.isEmpty(securityUser) || CollUtil.isEmpty(securityUser.getAuthorities())) {
            return false;
        }
        return hasPermissions(securityUser.getAuthorities(), permission);
    }

    /**
     * 验证用户是否不具备某权限，与 hasPerm逻辑相反
     *
     * @param permission 权限字符串
     * @return 用户是否不具备某权限
     */
    public boolean lacksPerm(String permission) {
        return !hasPerm(permission);
    }

    /**
     * 验证用户是否具有以下任意一个权限
     *
     * @param permissions 以 , 为分隔符的权限列表
     * @return 用户是否具有以下任意一个权限
     */
    public boolean hasAnyPerm(String permissions) {
        if (StrUtil.isBlank(permissions)) {
            return false;
        }
        SecurityUser securityUser = tokenService.getLoginUserFromToken(HttpContextUtil.getRequest());
        if (ObjectUtil.isEmpty(securityUser) || CollUtil.isEmpty(securityUser.getAuthorities())) {
            return false;
        }
        Set<String> authorities = securityUser.getAuthorities();
        for (String permission : permissions.split(StrPool.COMMA)) {
            if (permission != null && hasPermissions(authorities, permission)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断用户是否拥有某个角色
     *
     * @param role 角色字符串
     * @return 用户是否具备某角色
     */
    public boolean hasRole(String role) {
        if (StrUtil.isBlank(role)) {
            return false;
        }
        SecurityUser securityUser = tokenService.getLoginUserFromToken(HttpContextUtil.getRequest());
        if (ObjectUtil.isEmpty(securityUser) || CollUtil.isEmpty(securityUser.getRoleCodes())) {
            return false;
        }
        for (String roleName : securityUser.getRoleCodes()) {
            if (SUPER_ADMIN.equals(roleName) || StrUtil.equalsIgnoreCase(role, roleName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 验证用户是否不具备某角色，与 isRole逻辑相反。
     *
     * @param role 角色名称
     * @return 用户是否不具备某角色
     */
    public boolean lacksRole(String role) {
        return !hasRole(role);
    }

    /**
     * 验证用户是否具有以下任意一个角色
     *
     * @param roles 以 , 为分隔符的角色列表
     * @return 用户是否具有以下任意一个角色
     */
    public boolean hasAnyRoles(String roles) {
        if (StrUtil.isBlank(roles)) {
            return false;
        }
        SecurityUser securityUser = tokenService.getLoginUserFromToken(HttpContextUtil.getRequest());
        if (ObjectUtil.isEmpty(securityUser) || CollUtil.isEmpty(securityUser.getRoleCodes())) {
            return false;
        }
        for (String role : roles.split(StrPool.COMMA)) {
            if (hasRole(role)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否包含权限
     *
     * @param permissions 权限列表
     * @param permission  权限字符串
     * @return 用户是否具备某权限
     */
    private boolean hasPermissions(Set<String> permissions, String permission) {
        return permissions.contains(ALL_PERMISSION) || StrUtil.contains(StrUtil.join(StrPool.COMMA, permissions), permission);
    }
}
